package com.cykj.page01;

import java.util.Arrays;
import java.util.HashMap;

/**
 * @BelongsProject: HandTomcat
 * @BelongsPackage: com.cykj.page06
 * @Author: 阿隆
 * @CreateTime: 2025-10-18 16:47
 * @Description: 解析消息
 * @Version: 1.0
 */
public class MyHttpRequest {
    //请求消息
    private String requestMsg;
    //请求方法
    private String requestMethod;
    //请求URL
    private String requestUrl;
    //请求模块 /login?account=zhangsan&password=123456  ==>  /login
    private String requestModel;
    //协议版本
    private String protocol;
    //请求行
    private String requestLine;
    //请求体
    private String requestBody;
    //请求参数
    private HashMap<String, String> requestParam = new HashMap<>();
    //请求头参数
    private HashMap<String, String> requestHeadParam = new HashMap<>();


    public MyHttpRequest(String requestMsg) {
        this.requestMsg = requestMsg;
        //初步解析
        parse();
    }


    /**
     * @Description: 初步解析
     * @Param: []
     * @return: void
     */
    private void parse() {
        //通过回车换行符 切割获取第一个数据 ==> 请求行
        String[] splitRequestMsg = requestMsg.split("\r\n");

        //请求行 第一个数据 ==> 请求行
        requestLine = splitRequestMsg[0];

        //解析请求行
        parseRequestLine();


        //判断是否是POST请求
        if ("POST".equals(requestMethod)) {
            //解析请求体
            parseRequestBody();
        }

        //解析请求头
        parseRequestHead();
    }


    /**
     * @Description: 解析请求行
     * @Param: []
     * @return: void
     */
    private void parseRequestLine() {
        //通过空格切割获取请求方法 请求URL 请求协议版本
        String[] splitRequestLineArray = requestLine.split(" ");

        //请求方法
        requestMethod = splitRequestLineArray[0];
        //请求URL
        requestUrl = splitRequestLineArray[1];
        //协议版本
        protocol = splitRequestLineArray[2];

        //通过?切割获取一个元素 ==> 请求模块
        String[] splitRequestUrlArray = requestUrl.split("\\?");
        //获取请求模块  请求模块如果是静态资源的就跟请求url没有区别
        requestModel = splitRequestUrlArray[0];

        //判断这个请求是不是GET  ==> 引入现在有获取请求URL里面的请求参数
        if ("GET".equals(requestMethod)) {
            //判断有没有包含?
            if (requestUrl.contains("?")) {

                String[] splitRequestUrl = requestUrl.split("\\?", 2);

                //accout=zhangsan&password=123456
                String[] splitRequestParam = splitRequestUrl[1].split("&");

                for (String item : splitRequestParam) {

                    String[] value = item.split("=");

                    //防止只有请求参数 没有参数值
                    if (value.length > 1) {
                        requestParam.put(value[0], value[1]);
                    }


                }

                //   System.out.println(Arrays.toString(splitRequestUrl));
            }
        }

        //System.out.println("请求参数为: " + requestParam);


        //http://127.0.0.1:9090/login?account=zhangsan&password=123456
        //http://127.0.0.1:9090/pages/index.html
//        System.out.println("requestMethod = " + requestMethod);
//        System.out.println("requestUrl = " + requestUrl);
//        System.out.println("protocol = " + protocol);
//        System.out.println("requestModel = " + requestModel);

    }


    /**
     * @Description: 解析请求头
     * @Param: []
     * @return: void
     */
    private void parseRequestHead() {
        //通过两个回车换行符切割 获取第一个元素 (请求行 请求头)
        // 获取第一个元素再次通过回车换行符切割 去除第一个元素 剩下全是请求头

        //通过两个回车换行符切割消息
        String[] splitRequestBodyArray = requestMsg.split("\r\n\r\n");
        String[] splitRequestHeadArray = splitRequestBodyArray[0].split("\r\n");

        for (int i = 1; i < splitRequestHeadArray.length; i++) {
            //通过: 进行切割
            String[] value = splitRequestHeadArray[i].split(": ");
            if (value.length > 1) {
                requestHeadParam.put(value[0], value[1]);
            }

        }

      //  System.out.println(requestHeadParam);

    }


    /**
     * @Description: 解析请求体
     * @Param: []
     * @return: void
     */
    private void parseRequestBody() {
        //通过两个回车换行符切割消息
        String[] splitRequestBodyArray = requestMsg.split("\r\n\r\n");

        //判断有没有请求体
        if (splitRequestBodyArray.length > 1) {
            //account=zhangsan&password=123456
            //给请求体赋值
            requestBody = splitRequestBodyArray[1];

            String[] splitRequestBodyParam = requestBody.split("&");
            for (String item : splitRequestBodyParam) {
                String[] value = item.split("=");
                if (value.length > 1) {
                    requestParam.put(value[0], value[1]);
                }
            }
        }

      //  System.out.println("请求体的参数为: " + requestParam);


    }

    public String getRequestMsg() {
        return requestMsg;
    }

    public void setRequestMsg(String requestMsg) {
        this.requestMsg = requestMsg;
    }

    public String getRequestMethod() {
        return requestMethod;
    }

    public void setRequestMethod(String requestMethod) {
        this.requestMethod = requestMethod;
    }

    public String getRequestUrl() {
        return requestUrl;
    }

    public void setRequestUrl(String requestUrl) {
        this.requestUrl = requestUrl;
    }

    public String getRequestModel() {
        return requestModel;
    }

    public void setRequestModel(String requestModel) {
        this.requestModel = requestModel;
    }

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public String getRequestLine() {
        return requestLine;
    }

    public void setRequestLine(String requestLine) {
        this.requestLine = requestLine;
    }

    public String getRequestBody() {
        return requestBody;
    }

    public void setRequestBody(String requestBody) {
        this.requestBody = requestBody;
    }

    public HashMap<String, String> getRequestParam() {
        return requestParam;
    }

    public void setRequestParam(HashMap<String, String> requestParam) {
        this.requestParam = requestParam;
    }

    public HashMap<String, String> getRequestHeadParam() {
        return requestHeadParam;
    }

    public void setRequestHeadParam(HashMap<String, String> requestHeadParam) {
        this.requestHeadParam = requestHeadParam;
    }


    // 属性?  方法?
    /*
        请求消息
        请求行
        请求头
        请求体

    * 请求方法
    * 请求url
    * 协议版本
    请求头参数 key = value
    请求参数 => account=zhagnsan&password=123456   (127.0.0.1:9090/login?account=zhagnsan&password=123456)
    请求模块
    *


    方法:
       解析 ==> 请求行
       解析 ==> 请求头
       解析 ==> 请求体
    * */
}
